package com.fate.securecheckplatform.adapters;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.fate.securecheckplatform.R;
import com.fate.securecheckplatform.bean.TableForbiddenThing;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author:   RandBII
 * DATE:   2020/1/8
 * Description:录入物品的适配器
 */
public class AddRegisterAdapter extends RecyclerView.Adapter<AddRegisterAdapter.ViewHolder> implements View.OnClickListener {

    private Context context;
    private List<TableForbiddenThing> datas;
    private OnItemClickListener listener;

    public AddRegisterAdapter(Context context, List<TableForbiddenThing> datas) {
        this.context = context;
        this.datas = datas;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(context).inflate(R.layout.item_add_object, parent, false);
        return new ViewHolder(root, new CountTextWatcher(), new WeightTextWatcher());
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Log.e("nature--->", "" + holder.getLayoutPosition());
        TableForbiddenThing thing = datas.get(holder.getLayoutPosition());
        Log.e("nature--thing->", "" + thing.toString());


        holder.countTextWatcher.updatePosition(position);
        holder.weightTextWatcher.updatePosition(position);


        holder.mTvName.setText(TextUtils.isEmpty(thing.getWjpmc()) ? "" : thing.getWjpmc());
        if ("1".equals(thing.getWpcz()) || "移入暂存库".equals(thing.getWpcz())) {
            holder.mTvOperation.setText("移入暂存库");

        } else if ("2".equals(thing.getWpcz()) || "移入危险库".equals(thing.getWpcz())) {
            holder.mTvOperation.setText("移入危险库");
        } else if ("3".equals(thing.getWpcz()) || "民警处置".equals(thing.getWpcz())) {
            holder.mTvOperation.setText("民警处置");
        }

        holder.mTvOperation.setText(TextUtils.isEmpty(thing.getWpcz()) ? "" : thing.getWpcz());
        holder.mEtCount.setText(TextUtils.isEmpty(thing.getWjpsl()) ? "" : thing.getWjpsl());
        holder.mEtWeight.setText(TextUtils.isEmpty(thing.getWjpzl()) ? "" : thing.getWjpzl());

        if (TextUtils.isEmpty(thing.getWjpt())) {
            holder.mIvAddPic.setVisibility(View.VISIBLE);
            holder.mIvDeletePic.setVisibility(View.INVISIBLE);
            holder.mIvPic.setVisibility(View.INVISIBLE);
        } else {
            holder.mIvAddPic.setVisibility(View.INVISIBLE);
            holder.mIvDeletePic.setVisibility(View.VISIBLE);
            holder.mIvPic.setVisibility(View.VISIBLE);
            Glide.with(context).load(thing.getWjpt()).into(holder.mIvPic);
        }
        addListener(holder, holder.getLayoutPosition());

    }


    /**
     * 添加时间
     *
     * @param holder

     */
    private void addListener(@NonNull ViewHolder holder, int position) {

        holder.itemView.setTag(position);
        holder.mTvName.setTag(position);
        holder.mTvOperation.setTag(position);
        holder.mBtnDeleteOne.setTag(position);
        holder.mIvDeletePic.setTag(position);
        holder.mIvAddPic.setTag(position);
        holder.mEtCount.setTag(position);
        holder.mEtWeight.setTag(position);

        holder.mTvName.setOnClickListener(this);
        holder.mTvOperation.setOnClickListener(this);
        holder.mBtnDeleteOne.setOnClickListener(this);
        holder.mIvDeletePic.setOnClickListener(this);
        holder.mIvAddPic.setOnClickListener(this);


    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onItemClick(v, (Integer) v.getTag());
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }


    class CountTextWatcher implements TextWatcher {


        private int position;

        public void updatePosition(int p) {
            this.position = p;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            datas.get(position).setWjpsl(s.toString().trim());
        }
    }

    class WeightTextWatcher implements TextWatcher {

        private int position;

        public void updatePosition(int p) {
            this.position = p;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            datas.get(position).setWjpzl(s.toString().trim());
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_name)
        TextView mTvName;
        @BindView(R.id.tv_operation)
        TextView mTvOperation;
        @BindView(R.id.et_count)
        EditText mEtCount;
        @BindView(R.id.et_weight)
        EditText mEtWeight;
        @BindView(R.id.iv_add_pic)
        ImageView mIvAddPic;
        @BindView(R.id.iv_pic)
        ImageView mIvPic;
        @BindView(R.id.btn_delete_one)
        Button mBtnDeleteOne;
        @BindView(R.id.iv_delete_pic)
        ImageView mIvDeletePic;

        CountTextWatcher countTextWatcher;
        WeightTextWatcher weightTextWatcher;

        public ViewHolder(@NonNull View itemView, CountTextWatcher countTextWatcher, WeightTextWatcher weightTextWatcher) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.countTextWatcher = countTextWatcher;
            this.weightTextWatcher = weightTextWatcher;
            mEtWeight.addTextChangedListener(weightTextWatcher);
            mEtCount.addTextChangedListener(countTextWatcher);
        }

    }
}
